<template>
  <div class="html-preview">
    <div class="preview-header">
      <div class="header-content">
        <h1>🚀 HTML预览</h1>
        <p>Java学习平台 - 代码实时预览</p>
      </div>
      <div class="header-actions">
        <button class="refresh-btn" @click="refreshPreview" title="刷新预览">
          🔄 刷新
        </button>
        <button class="close-btn" @click="closeWindow" title="关闭标签页">
          ✕ 关闭
        </button>
      </div>
    </div>

    <div class="preview-container">
      <div class="preview-content" v-html="htmlContent"></div>
    </div>

    <div class="preview-footer">
      <span class="footer-text">
        ⚡ 由 Java学习平台 提供技术支持
      </span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const htmlContent = ref('')

// 从sessionStorage中获取HTML内容
onMounted(() => {
  try {
    const storedHtml = sessionStorage.getItem('htmlPreviewContent')
    if (storedHtml) {
      htmlContent.value = storedHtml
      // 获取后立即清除，避免数据残留
      sessionStorage.removeItem('htmlPreviewContent')
    } else {
      htmlContent.value = '<div style="text-align: center; padding: 50px; color: #666;"><h2>😕 没有找到要预览的HTML内容</h2><p>请返回代码页面重新点击运行按钮</p></div>'
    }
  } catch (error) {
    console.error('获取HTML内容失败:', error)
    htmlContent.value = '<div style="text-align: center; padding: 50px; color: #e74c3c;"><h2>❌ 预览内容加载失败</h2><p>请检查浏览器设置或重新尝试</p></div>'
  }
})

// 刷新预览
function refreshPreview() {
  window.location.reload()
}

// 关闭标签页
function closeWindow() {
  // 尝试关闭当前标签页
  if (window.history.length > 1) {
    window.history.back()
  } else {
    window.close()
  }
}
</script>

<style scoped>
.html-preview {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf5 100%);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.preview-header {
  background: linear-gradient(120deg, #42b983, #2c3e50);
  color: white;
  padding: 20px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.header-content h1 {
  margin: 0 0 5px 0;
  font-size: 1.8rem;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.header-content p {
  margin: 0;
  font-size: 0.95rem;
  opacity: 0.9;
  font-weight: 300;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.refresh-btn,
.close-btn {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.refresh-btn:hover,
.close-btn:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.close-btn:hover {
  background: rgba(231, 76, 60, 0.8);
  border-color: rgba(231, 76, 60, 0.9);
}

.preview-container {
  flex: 1;
  padding: 30px;
  overflow: auto;
}

.preview-content {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  min-height: calc(100vh - 200px);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.preview-footer {
  background: rgba(255, 255, 255, 0.9);
  padding: 15px 30px;
  text-align: center;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.footer-text {
  color: #666;
  font-size: 0.85rem;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .preview-header {
    padding: 15px 20px;
    flex-direction: column;
    gap: 15px;
    text-align: center;
  }

  .header-content h1 {
    font-size: 1.5rem;
  }

  .header-actions {
    justify-content: center;
  }

  .preview-container {
    padding: 20px;
  }

  .preview-content {
    padding: 20px;
  }

  .preview-footer {
    padding: 12px 20px;
  }
}

/* 确保预览内容中的样式正常显示 */
.preview-content :deep(*) {
  max-width: 100%;
}

.preview-content :deep(img) {
  max-width: 100%;
  height: auto;
}

.preview-content :deep(table) {
  width: 100%;
  border-collapse: collapse;
}

.preview-content :deep(pre) {
  overflow-x: auto;
  white-space: pre-wrap;
  word-wrap: break-word;
}
</style>
